/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.shop;

import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.Zone;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import extension.game.zone.GameZoneExtension;
import java.sql.SQLException;

/**
 *
 * @author Hung
 */
public class Shop {

    public static final int ERROR = 0;
    public static final int NOT_MONEY = 1;
    public static final int SUCCESS = 2;
    private Item[] items = null;
    private TypeItem[] types = null;
    private Zone zone;
    public static String itemsTableName = "pocker.items";
    public static String itemsTypeTableName = "pocker.items_type";
    public static String moneyQueueTableName = "pocker.money_queue_poker";

    public Shop(Zone zone) {
        this.zone = zone;
        GameZoneExtension gameZoneExtension = (GameZoneExtension) zone.getExtension();
        String roomPrefix = gameZoneExtension.getRoomPrefix();
        if (roomPrefix.equals("P")) {
            moneyQueueTableName = "money_queue_phom";
        } else if (roomPrefix.equals("TL")) {
            moneyQueueTableName = "money_queue_tienlen";
        } else if (roomPrefix.equals("TLMN")) {
            moneyQueueTableName = "money_queue_tienlenmiennam";
        }
    }

    private void loadItemData() {
        IDBManager dbManager = zone.getDBManager();
        String sql = "SELECT * FROM " + itemsTableName;
        try {
            ISFSArray rows = dbManager.executeQuery(sql);
            if (rows.size() > 0) {
                items = new Item[rows.size()];
                for (int i = 0; i < items.length; i++) {
                    int id = rows.getSFSObject(i).getInt("id");
                    int kind = rows.getSFSObject(i).getInt("kind");
                    String name = rows.getSFSObject(i).getUtfString("name");
                    long price = rows.getSFSObject(i).getLong("price");
                    String image = rows.getSFSObject(i).getUtfString("image");
                    String describe = rows.getSFSObject(i).getUtfString("describ");
                    items[i] = new Item(id, kind, name, price, image, describe);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void loadTypeData() {
        IDBManager dbManager = zone.getDBManager();
        String sql = "SELECT * FROM " + itemsTypeTableName;
        try {
            ISFSArray rows = dbManager.executeQuery(sql);
            if (rows.size() > 0) {
                types = new TypeItem[rows.size()];
                for (int i = 0; i < types.length; i++) {
                    int kind = rows.getSFSObject(i).getInt("id");
                    String name = rows.getSFSObject(i).getUtfString("name");
                    types[i] = new TypeItem(kind, name);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Item[] getItemData() {
        int count = 0;
        while (items == null && count < 10) {
            count++;
            loadItemData();
        }
        return items;
    }

    public TypeItem[] getTypeData() {
        int count = 0;
        while (types == null && count < 10) {
            count++;
            loadTypeData();
        }
        return types;
    }

    public int buyItem(int id, int number, User user) {
        long userMoney = 50000l;
        long totalSpendMoney = 0l;
        IDBManager dbManager = zone.getDBManager();
        String sql = "SELECT money FROM " + moneyQueueTableName + " WHERE username = ?";
        try {
            ISFSArray rows = dbManager.executeQuery(sql, new Object[]{user.getName()});
            if (rows.size() > 0) {
                userMoney = rows.getSFSObject(0).getLong("money");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < items.length; i++) {
            if (items[i].getId() == id) {
                totalSpendMoney = number * items[i].getPrice();
                if (totalSpendMoney <= userMoney) {
                    sql = "UPDATE " + moneyQueueTableName + " SET money = ? WHERE username = ?";
                    try {
                        userMoney -= totalSpendMoney;
                        dbManager.executeUpdate(sql, new Object[]{userMoney, user.getName()});
                        return SUCCESS;
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                } else {
                    return NOT_MONEY;
                }
                break;
            }
        }
        return ERROR;
    }
}